import type { EntityProps } from '../types/crud';

import { set } from '@vben/utils';

import { decorator2Prop } from '#/constant';
import { camelize } from '#/utils';

/**
 * @Description:实体名称装饰器
 * @Author: li shi bin
 * @Date: 10:04 2025/1/16
 * @version : V0.0.1-beta
 */

/**
 * 实体名称装饰器
 * @param prop   实体属性 @Link EntityProps
 * @returns 实体装饰器
 */
export default function Entity(prop?: EntityProps) {
  return function (origin: any) {
    const Target = origin;
    const target: any = function (...args: any[]) {
      const instance = new Target(...args);
      // 将 entityProps 挂载到实例上（实例独有）
      const constructorName = instance.constructor.name;
      instance[decorator2Prop.name] = prop?.module ?? camelize(constructorName);
      set(instance, decorator2Prop.entityAttrs, prop);
      return instance;
    };
    // 复制原型链
    target.prototype = Target.prototype;
    return target;
  };
}
